
/** 初始化云开发环境 */
wx.cloud.init({
  env:'dict-0gpzxpi132897f1b',
  traceUser:true
})
/**例句查询匹配 */
async function sentenceQuery(word,appid="wx2facb7b57eedf7e6"){
  console.log(word+':对应例句匹配')
  let res = await wx.cloud.callFunction({
    name:'sentence-api',
    data:{
      word,
      appid
    }
  })
  if(res.result.errCode == -1){
    wx.showToast({
      title: res.result.errMsg,
      icon:'none'
    })
  }
  // console.log(res)
  return res.result;
}
/**
 * 查询单个单词信息 -- TIPS
 * @param {*} word 
 */
async function wordQuery(word){
  let res = await wx.cloud.callFunction({
    name:'word-api',
    data:{
      action:'match',
      word
    }
  })
  if(res.result.errCode == -1){
    wx.showToast({
      title: res.result.errMsg,
      icon:'none'
    })
  }
  // console.log(res)
  return res.result;
}
/** 冗余@WordQuery */
async function fitMatchWord(word){
  return await wordQuery(word);
}

/**
 * @api 单词前缀匹配接口
 * @param {*} word 
 */
async function preMatchWord(word) {
  let res = await wx.cloud.callFunction({
    name:'word-api',
    data:{
      action:'prematch',
      word
    }
  })
  if(res.result.errCode == -1){
    wx.showToast({
      title: res.result.errMsg,
      icon:'none'
    })
  }
  // console.log(res)
  return res.result;
}
/**
 * @api 英汉互译
 * @param {*} sentence 
 */
async function translationEnZh(sentence) {
  let res = await wx.cloud.callFunction({
    name:'translate',
    data:{
      action:'zh-en',
      words:sentence
    }
  })
  if(res.result.errCode == -1){
    wx.showToast({
      title: res.result.errMsg,
      icon:'none'
    })
  }
  // console.log(res)
  return res.result;
}
/**
 * @api 获取当前用户收藏的单词列表
 */
async function getFavorWord(userid) {
  console.log('传入userId:'+userid)
  let res = await wx.cloud.callFunction({
    name:'word-favor-api',
    data:{
      action:'findAll',
      userId:userid
    }
  })
  return res.result;
  // let res = await
}
/**@新增收藏 */
async function addFavor(userId,word){
  let res = await wx.cloud.callFunction({
    name:'word-favor-api',
    data:{
      action:'addOne',
      userId,
      word
    }
  })
  return res.result;
}
/**@判定是不是收藏 */
async function isFavor(userId,word){
  let res = await wx.cloud.callFunction({
    name:'word-favor-api',
    data:{
      action:'findOne',
      userId,
      word
    }
  })
  return res.result;
}
/**@移除收藏 */
async function pullFavor(userId,word){
  let res = await wx.cloud.callFunction({
    name:'word-favor-api',
    data:{
      action:'pullOne',
      userId,
      word
    }
  })
  return res.result;
}
/**@获取单词册信息 */
async function getCateList(){
  let res = await wx.cloud.callFunction({
    name:'cate-api',
    data:{
      action:'findAll'
    }
  })
  return res.result;
}
async function translationTo(target, words) {
  let res = await wx.cloud.callFunction({
    name:'translate',
    data:{
      action:'target-translate',
      language:target,
      words:words
    }
  })
  if(res.result.errCode == -1){
    wx.showToast({
      title: res.result.errMsg,
      icon:'none'
    })
  }
  // console.log(res)
  return res.result;
}
function TimeCode() {
  var date = new Date();
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
//获取日期
function _formatTime(time) {
  var date = time.getFullYear() + '年' + time.getMonth() + '月' + time.getDate() + '日'
  var ftime = time.getHours() + '时' + time.getMinutes() + '分' + time.getSeconds() + '秒'
  return date + ftime;
}
function TimeCodeYmd(){
  var date = new Date();
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  return [year, month, day].map(formatNumber).join('-');
}
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
module.exports = {
  preMatchWord,
  translationEnZh,
  getFavorWord,
  translationTo,
  wordQuery,
  sentenceQuery,
  fitMatchWord,
  isFavor,
  getCateList,
  addFavor,
  pullFavor,
  TimeCode,
  TimeCodeYmd
}